// Package article
// Created by GoLand
// @User: lenora
// @Date: 2024/7/12
// @Time: 17:22

package article

import (
	"encoding/json"
	"github.com/IBM/sarama"
)

const (
	topic = "article_read"
	biz   = "article"
)

type Producer interface {
	ReadEvent(evt ReadEvent) error
}

type ReadEvent struct {
	UserId    uint64 `json:"user_id"`
	ArticleId uint64 `json:"article_id"`
}

type SaramaSyncProducer struct {
	producer sarama.SyncProducer
}

func NewSaramaSyncProducer(producer sarama.SyncProducer) Producer {
	return &SaramaSyncProducer{producer: producer}
}

func (p *SaramaSyncProducer) ReadEvent(evt ReadEvent) error {
	msg, err := json.Marshal(evt)
	if err != nil {
		return err
	}
	_, _, err = p.producer.SendMessage(&sarama.ProducerMessage{
		Topic: topic,
		Value: sarama.StringEncoder(msg),
	})
	return err
}
